using System;
using System.Collections.Generic;

namespace CSharpPro._1106
{
    public class UserService : IUserService
    {
        private List<User> _users = new List<User>
        {
            new User("zyl", "zyl20060903") // 预置测试用户
        };

        public bool Register(User user)
        {
            // 校验用户名是否已存在（foreach遍历泛型集合）
            foreach (var existingUser in _users)
            {
                if (existingUser.Username == user.Username)
                {
                    Console.WriteLine("用户名已存在，请重新注册！");
                    return false;
                }
            }
            _users.Add(user);
            Console.WriteLine("注册成功！");
            return true;
        }

        public User Login(string username, string password)
        {
            // foreach遍历泛型集合校验登录
            foreach (var user in _users)
            {
                if (user.Username == username && user.Password == password)
                {
                    Console.WriteLine("登录成功！");
                    return user;
                }
            }
            Console.WriteLine("用户名或密码错误！");
            return null;
        }

        public void ShowMenu()
        {
            Console.WriteLine("\n===== 主菜单 =====");
            Console.WriteLine("1. 登录");
            Console.WriteLine("2. 注册");
            Console.WriteLine("3. 退出");
            Console.Write("请选择功能（1-3）：");
        }

        public void Run()
        {
            bool isRunning = true;
            while (isRunning)
            {
                ShowMenu();
                string choice = Console.ReadLine();
                switch (choice)
                {
                    case "1":
                        Console.Write("请输入用户名：");
                        string loginUsername = Console.ReadLine();
                        Console.Write("请输入密码：");
                        string loginPassword = Console.ReadLine();
                        Login(loginUsername, loginPassword);
                        break;
                    case "2":
                        try
                        {
                            Console.Write("请输入用户名：");
                            string regUsername = Console.ReadLine();
                            Console.Write("请输入密码：");
                            string regPassword = Console.ReadLine();
                            Console.Write("请确认密码：");
                            string confirmPassword = Console.ReadLine();
                            if (regPassword != confirmPassword)
                            {
                                Console.WriteLine("两次密码输入不一致！");
                                break;
                            }
                            User newUser = new User(regUsername, regPassword);
                            Register(newUser);
                        }
                        catch (ArgumentException ex)
                        {
                            Console.WriteLine(ex.Message);
                        }
                        break;
                    case "3":
                        isRunning = false;
                        Console.WriteLine("系统已退出！");
                        break;
                    default:
                        Console.WriteLine("请输入有效数字！");
                        break;
                }
            }
        }
    }
}